﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace KingApp.Model
{
    [DataContract]
    public class Lottery
    {
        //public Lottery()
        //{
        //    this.coms.AddRange(this.GetComs());
        //    this.coms.AddRange(this.GetComs());
        //}

        public int Id { get; set; }
        public int CodeFrist
        {
            get { return this.Codes[0]; }

        }

        public int CodeSecond
        {
            get { return this.Codes[1]; }

        }

        public int CodeThrid
        {
            get { return this.Codes[2]; }

        }

        public int CodeFourth
        {
            get { return this.Codes[3]; }

        }

        public int CodeFifh
        {
            get { return this.Codes[4]; }

        }

        [DataMember(Name = "openTerm")]
        public string Issue { get; set; }

        [DataMember(Name = "openResult")]
        public string LotteryNumber { get; set; }

        public string Lottery012
        {
            get
            {
                return string.Join(",", this.Codes.Select(l => l % 3).ToArray());
            }
        }

        public string Lottery0123
        {
            get
            {
                return string.Join(",", this.Codes.Select(l => l % 4).ToArray());
            }
        }

        public string Lottery01234
        {
            get
            {
                return string.Join(",", this.Codes.Select(l => l % 6).ToArray());
            }
        }

        [DataMember(Name = "openTermDT")]
        public string Time { get; set; }

        /// <summary>
        /// 和值
        /// </summary>
        public int Sum
        {
            get
            {
                return this.Codes.Sum();
            }
        }

        /// <summary>
        /// 合值（和尾）
        /// </summary>
        public int SumTail
        {
            get
            {
                return this.Sum % 10;
            }
        }

        /// <summary>
        /// 中间值
        /// </summary>
        public int MidCode
        {
            get
            {
                return this.Codes.OrderBy(l => l).ToArray()[2];
            }
        }

        public string OddCode
        {
            get
            {
                List<int> odd = new List<int>();

                foreach (var c in this.Codes)
                {
                    if ((c % 2) == 1)
                    {

                        odd.Add(c);
                    }

                }


                return string.Join(",", odd.OrderBy(o => o).ToArray());
            }

        }

        public string EvenCode
        {
            get
            {
                string strOdd = string.Empty, strEven = string.Empty;

                foreach (var c in this.Codes)
                {
                    if ((c % 2) == 1)
                    {

                        strOdd = string.Concat(strOdd, ",", c.ToString());
                    }
                    else
                    {

                        strEven = string.Concat(strEven, ",", c.ToString());
                    }
                }


                return strEven;
            }
        }


        public string OddEven
        {
            get
            {

                int odd = 0, even = 0;
                string strOdd = string.Empty, strEven = string.Empty;

                foreach (var c in this.Codes)
                {
                    if ((c % 2) == 1)
                    {
                        odd++;
                        strOdd = string.Concat(strOdd, ",", c.ToString());
                    }
                    else
                    {
                        even++;
                        strEven = string.Concat(strEven, ",", c.ToString());
                    }
                }

                return string.Concat(odd, ":", even);
            }
        }

        private int[] Codes
        {
            get
            {
                if (string.IsNullOrEmpty(this.LotteryNumber))
                {
                    return new int[] { 0, 0, 0, 0, 0 };
                }
                else
                {
                    var arry = this.LotteryNumber.Split(',');
                    if (arry.Length < 5)
                    {
                        return null;
                    }

                    return arry.Select(l => int.Parse(l)).ToArray();
                }
            }
        }

        private int[] SortedCodes
        {
            get { return this.Codes.OrderBy(c => c).ToArray(); }
        }

        public int CorrectCount
        {
            get
            {
                int count = 0;

                var max = coms.Select(a => { count = 0; return a.Select(a1 => this.SortedCodes.Contains(a1) ? ++count : count).Max(); }).ToList();
                //var cc = max.Where(m => m == 3).Count() + max.Where(m => m == 4).Count() * 4 + max.Where(m => m == 5).Count() * 10;                
                return max.FirstOrDefault();

            }
        }

        private List<int[]> coms = new List<int[]>{  
            new int[]{2,3,8,9}                      
                                    //new int[]{2,9,6,3,11},
                                    //new int[]{2,6,4,5,7},
                                    //new int[]{2,8,10,4,1},
                                    //new int[]{2,8,10,5,7},
                                    //new int[]{9,6,8,1,7},
                                    //new int[]{9,3,10,4,11},
                                    //new int[]{9,3,5,1,11},
                                    //new int[]{6,3,8,4,7},
                                    //new int[]{6,3,1,11,7},
                                    //new int[]{6,8,10,4,5}
        };


        private List<int[]> GetComs()
        {
            Dictionary<string, int> srC = new Dictionary<string, int>();

            Random r = new Random();

            int[] reds = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            List<int> lint = new List<int>();
            do
            {
                var x = r.Next(1, 11);
                if (!lint.Contains(x))
                {
                    lint.Add(x);
                }

            } while (lint.Count < 7);

            for (var i = 0; i < 11; i++)
            {
                if (i < 6)
                {
                    reds[i] = lint[i];
                }
                else
                {
                    for (var j = 1; j < 12; j++)
                    {
                        if (!reds.Contains(j))
                        {
                            reds[i] = j;
                            break;
                        }
                    }
                }
            }


            if (reds.Count() >= 11)
            {
                srC.Add("C", reds[0]);//6
                srC.Add("D", reds[1]);//5
                srC.Add("E", reds[2]);//5
                srC.Add("B", reds[3]);//5
                srC.Add("G", reds[4]);//5
                srC.Add("K", reds[5]);//4
                srC.Add("H", reds[6]);//4 
                srC.Add("F", reds[7]);//4 
                srC.Add("A", reds[8]);//4 
                srC.Add("I", reds[9]);//4 
                srC.Add("J", reds[10]);//4 
            }

            List<int[]> data = new List<int[]>();
            data.Add(new int[] { srC["A"], srC["B"], srC["C"], srC["D"], srC["J"] });
            data.Add(new int[] { srC["A"], srC["C"], srC["G"], srC["H"], srC["K"] });
            data.Add(new int[] { srC["A"], srC["E"], srC["F"], srC["G"], srC["I"] });
            data.Add(new int[] { srC["A"], srC["E"], srC["F"], srC["H"], srC["K"] });
            data.Add(new int[] { srC["B"], srC["C"], srC["E"], srC["I"], srC["K"] });
            data.Add(new int[] { srC["B"], srC["D"], srC["F"], srC["G"], srC["J"] });
            data.Add(new int[] { srC["B"], srC["D"], srC["H"], srC["I"], srC["J"] });
            data.Add(new int[] { srC["C"], srC["D"], srC["E"], srC["G"], srC["K"] });
            data.Add(new int[] { srC["C"], srC["D"], srC["I"], srC["J"], srC["K"] });
            data.Add(new int[] { srC["C"], srC["E"], srC["F"], srC["G"], srC["H"] });

            return data;

        }

        public bool HasCode(int[] codes)
        {
            foreach (var c in codes)
            {
                if (!this.Codes.Contains(c))
                    return false;
            }
            return true;
        }

        public int HasCodeCount(int[] codes)
        {
            int count = 0;
            foreach (var c in codes)
            {
                if (this.Codes.Contains(c))
                    count++;
            }

            return count;
        }

    }

}
